package com.zhilianbao.leyaogo.view.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zhilianbao.leyaogo.utils.Utils;

import java.util.List;

/**
 * ClassName: BaseContainer <br>
 * Description: 基本容器<br>
 * Author: Cyarie <br>
 * Created: 2016/11/2 10:53 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public abstract class BaseContainer<T extends Object> extends LinearLayout {

    private int MAX_WIDTH = 0;

    //Item集合
    private List<T> mList;

    /**
     * 长度 单位为Pixel
     **/
    private int pxOneMaxWandH;  // 单个功能最大允许宽高
    private int pxMoreWandW = 0;// 多个功能的宽
    private int pxViewPadding = 0 /*Utils.dp2px(4.0f)*/;// 间距

    private int MAX_PER_ROW_COUNT = 4;// 每行显示最大数

    private LinearLayout.LayoutParams onePara;//单个Item的LayoutParams
    private LinearLayout.LayoutParams morePara, moreParaColumnFirst;//多个功能的LayoutParams
    private LinearLayout.LayoutParams rowPara;//每行LayoutParams

    private Context mContext;


    public BaseContainer(Context context) {
        super(context);
        this.setOrientation(VERTICAL);
        mContext = context;
    }

    public BaseContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOrientation(VERTICAL);
        mContext = context;
    }


    /**
     * 设置列表集合
     *
     * @param lists
     * @throws IllegalArgumentException
     */
    public void setList(List<T> lists) throws IllegalArgumentException {
        if (lists == null) {
            throw new IllegalArgumentException("list is null...");
        }
        mList = lists;
        if (MAX_WIDTH > 0) {
            pxMoreWandW = (MAX_WIDTH - pxViewPadding * (MAX_PER_ROW_COUNT - 1)) / MAX_PER_ROW_COUNT;
            pxOneMaxWandH = MAX_WIDTH * 2 / MAX_PER_ROW_COUNT;
            initLayoutParams();
        }

        initView();
    }

    /**
     * 初始化LayoutParams
     */
    private void initLayoutParams() {
        int wrap = LinearLayout.LayoutParams.WRAP_CONTENT;
        int match = LinearLayout.LayoutParams.MATCH_PARENT;
        onePara = new LinearLayout.LayoutParams(wrap, wrap);
        moreParaColumnFirst = new LinearLayout.LayoutParams(pxMoreWandW, ViewGroup.LayoutParams.WRAP_CONTENT);
        morePara = new LinearLayout.LayoutParams(pxMoreWandW, ViewGroup.LayoutParams.WRAP_CONTENT);
        morePara.setMargins(pxViewPadding, 0, 0, 0);
        rowPara = new LinearLayout.LayoutParams(match, wrap);
    }

    /**
     * 根据列表数量初始化不同的View布局,还要为每一个View作点击效果
     */
    private void initView() {
        this.removeAllViews();
        if (mList == null || mList.size() == 0) {
            return;
        }
        if (mList.size() == 1) {
            addView(createView(0, false));
        } else {
            int allCount = mList.size();
            int rowCount = allCount / MAX_PER_ROW_COUNT
                    + (allCount % MAX_PER_ROW_COUNT > 0 ? 1 : 0);// 行数
            for (int rowCursor = 0; rowCursor < rowCount; rowCursor++) {
                LinearLayout rowLayout = new LinearLayout(getContext());
                rowLayout.setOrientation(LinearLayout.HORIZONTAL);

                rowLayout.setLayoutParams(rowPara);
                if (rowCursor != 0) {
                    rowLayout.setPadding(0, pxViewPadding, 0, 0);
                }

                int columnCount = allCount % MAX_PER_ROW_COUNT == 0 ? MAX_PER_ROW_COUNT
                        : allCount % MAX_PER_ROW_COUNT;//每行的列数
                if (rowCursor != rowCount - 1) {
                    columnCount = MAX_PER_ROW_COUNT;
                }
                addView(rowLayout);

                int rowOffset = rowCursor * MAX_PER_ROW_COUNT;// 行偏移
                for (int columnCursor = 0; columnCursor < columnCount; columnCursor++) {
                    int position = columnCursor + rowOffset;
                    rowLayout.addView(createView(position, true));
                }
            }
        }
    }


    /**
     * 创建View
     *
     * @param position 位置
     * @param isMulti  是否多Item
     * @return
     */
    private View createView(int position, final boolean isMulti) {
        T item = mList.get(position);
        View view = View.inflate(mContext, getLayoutResId(), null);
        if (getLayoutResId() == 0) {
            throw new NullPointerException("the getLayoutResId() must not be null");
        }
        if (isMulti) {
            view.setLayoutParams(position % MAX_PER_ROW_COUNT == 0 ? moreParaColumnFirst : morePara);
        } else {
            view.setLayoutParams(onePara);
        }
        initView(view, position, item);

        return view;
    }

    public abstract int getLayoutResId();


    public abstract void initView(View view, int position, T item);


    /**
     * 动态设置宽
     *
     * @param width
     */
    public void setWidth(int width) {
        if (width != 0) {
            Utils.putHomeColumnContainer(width);
        }
        this.MAX_WIDTH = Utils.getHomeColumnContainer();
    }
}
